#!/usr/bin/python2.4
# -*- coding: utf-8 -*-
#
#    Pydapt - Adaptive Devices Framework for Python
#    Copyright (C) 2009  Rodolpho Eckhardt <rodolpho@eckhardt.com.br>
#
#    This program is free software: you can redistribute it and/or modify
#    it under the terms of the GNU General Public License as published by
#    the Free Software Foundation, either version 3 of the License, or
#    (at your option) any later version.
#
#    This program is distributed in the hope that it will be useful,
#    but WITHOUT ANY WARRANTY; without even the implied warranty of
#    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#    GNU General Public License for more details.
#
#    You should have received a copy of the GNU General Public License
#    along with this program.  If not, see <http://www.gnu.org/licenses/>.
#
#    You can contact the Pydapt team at pydapt@googlegroups.com
#
"""Unit tests for base interfaces for framework.
"""

__author__ = 'rodolpho@eckhardt.com.br (Rodolpho Eckhardt)'


import base
import unittest


class DeviceTest(unittest.TestCase):
  """Tests the Device object."""

  def testFindRule(self):
    """Tests the FindRule method."""
    c1 = base.Configuration()
    c2 = base.Configuration()
    c3 = base.Configuration()
    r1 =base.Rule(c1, 'a', c2, 'b')
    r2 = base.Rule(c1, 'b', c3, 'a')
    r3 = base.Rule(c2, 'a', c3, 'a')
    input_alphabet = output_alphabet = 'ab'
    rules = [r1, r2, r3]
    d = base.Device([c1, c2, c3], input_alphabet, output_alphabet, c1, [c2],
                    rules)
    d.current_configuration = c1
    r = d.FindRules('a')
    self.assertEquals(r[0], r1)
    r = d.FindRules('b')
    self.assertEquals(r[0], r2)
    d.current_configuration = c2
    r = d.FindRules('a')
    self.assertEquals(r[0], r3)
    r = d.FindRules('c')
    self.assertEquals(len(r), 0)

  def testInit(self):
    """Tests the initialization of the object."""
    configurations = [1, 2, 3]
    initial_configuration = 2
    rules = ['a', 'b', 'c',]
    input_alphabet = '1234567'
    output_alphabet = 'abcdef'
    acceptance_configurations = [2, 3]
    d = base.Device(configurations, input_alphabet, output_alphabet,
                    initial_configuration, acceptance_configurations, rules)
    self.assertEquals(d.configurations, configurations)
    self.assertEquals(d.initial_configuration, d.initial_configuration)
    self.assertEquals(d.rules, rules)
    self.assertEquals(d.acceptance_configurations, acceptance_configurations)
    self.assertEquals(d.current_configuration, d.initial_configuration)
    d = base.Device(configurations, input_alphabet, output_alphabet,
                    initial_configuration, acceptance_configurations, rules, 3)
    self.assertEquals(d.configurations, configurations)
    self.assertEquals(d.initial_configuration, d.initial_configuration)
    self.assertEquals(d.rules, rules)
    self.assertEquals(d.acceptance_configurations, acceptance_configurations)
    self.assertEquals(d.current_configuration, 3)

  def testIsAccepting(self):
    """Tests the IsAccepting method."""
    c1 = base.Configuration()
    c2 = base.Configuration()
    d = base.Device([c1, c2], '', '', c1, [c1,], [])
    d.current_configuration = c1
    self.assertTrue(d.IsAccepting())
    d.current_configuration = c2
    self.assertFalse(d.IsAccepting())

  def testReset(self):
    """Tests the Reset method."""
    c1 = base.Configuration()
    c2 = base.Configuration()
    d = base.Device([c1, c2], '', '', c1, [c1,], [])
    self.assertEquals(d.current_configuration, c1)
    d.current_configuration = c2
    self.assertEquals(d.current_configuration, c2)
    d.Reset()
    self.assertEquals(d.current_configuration, c1)


class RuleTest(unittest.TestCase):
  """Tests the Rule object."""

  def testInit(self):
    """Tests the initialization of the object."""
    current_configuration = 1
    input_symbol = 'c'
    next_configuration = 2
    output_symbol = 'E'
    r = base.Rule(current_configuration, input_symbol, next_configuration,
                  output_symbol)
    self.assertEquals(r.current_configuration, current_configuration)
    self.assertEquals(r.input_symbol, input_symbol)
    self.assertEquals(r.next_configuration, next_configuration)
    self.assertEquals(r.output_symbol, output_symbol)


if __name__ == '__main__':
  unittest.main()
